//
//  AppDelegate.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/12.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    lazy var selector:SinaPictureSelectorVC = {
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.itemSize = CGSize(width: selectorCellWidth, height: selectorCellWidth)
        //设置行和列的最小间距
        flowlayout.minimumLineSpacing = selectorMargin
        flowlayout.minimumInteritemSpacing = selectorMargin
        //设置组内间距
        flowlayout.sectionInset = UIEdgeInsets(top: selectorMargin, left: selectorMargin, bottom: 0, right: selectorMargin)
        let picSel = SinaPictureSelectorVC(collectionViewLayout: flowlayout)
        return picSel
        
    }()
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //注册通知
        registerNotification()
        //实例化根控制器
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //设置根视图控制器
//        window?.rootViewController = SinaMainTabBarVC()
//        window?.rootViewController = SinaWelcomVC()
        //登录显示欢迎页面,没登录加载访客视图页面
        window?.rootViewController = SinaUserAccountViewModel.shared.userLogin ? SinaWelcomVC():SinaMainTabBarVC()
//        let nav = UINavigationController(rootViewController: selector)
//        window?.rootViewController = nav
        
//        let compose = SinaComposeVCViewController()
//        let nav = UINavigationController(rootViewController: compose)
//        window?.rootViewController = nav

        

//        window?.rootViewController = SinaHomeTableVC()
        //可视化window
        window?.makeKeyAndVisible()
        
        
        return true
    }
    //MARK: -
    //MARK: - 注册通知
    private func registerNotification(){
        
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootVC(n:)), name: NSNotification.Name(kChoseRootViewController), object: nil)
    }
    //MARK: -
    //MARK: - 切换根视图控制器
    @objc private func switchRootVC(n:Notification){
    
    //根据通知对象中object来判断显示设么控制器
        let vc = n.object == nil ? SinaMainTabBarVC():SinaWelcomVC()
        window?.rootViewController = vc
    
    }
    //MARK: -
    //MARK: - 移除通知(仅只是规范,无意义)
    deinit {
        
        NotificationCenter.default.removeObserver(self)
    }
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

